package com.heather.exam.until;

import com.heather.exam.pojo.table.Options;
import com.heather.exam.pojo.vo.OptionsVO;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class StingUtilHkj {
    public static String YZM(){
        ArrayList<Character> list=new ArrayList();
        int k=4;
        String[] n=new String[4];
        Random r=new Random();
        StringBuilder sb=new StringBuilder();
        int a=0;

        for (int i = 0; i < k; i++) {
            a=r.nextInt(26)+65;
            list.add((char)a);
            a=r.nextInt(26)+97;
            list.add((char)a);
            Collections.shuffle(list);
        }

        for (int i = 0; i < k; i++) {
            n[i]= list.get(r.nextInt(list.size())).toString();
            sb.append(n[i]);
        }

        return sb.toString();
    }

    public static int getNumberFromString(String str){
        char[] chars=str.toCharArray();
        StringBuilder builder=new StringBuilder();
        for(char c:chars){
            if(c>='0'&&c<='9'){
                builder.append(c);
            }
        }
        return Integer.valueOf(builder.toString());
    }

    public static String numberToLen2String(int i){
        if(-1<i&&i<10){
            return"0"+i;
        }else{
            return i+"";
        }
    }
    public static Integer stringPriceToInt(String price) throws Exception{
        if(price != null){
            try{
                Double d = Double.valueOf(price);
                return Double.valueOf(d * 100).intValue();
            }catch (NumberFormatException e){
                throw new Exception("不是浮点数格式");
            }
        }
        return null;
    }
    public static List<Options> parseData(String data) {
        List<Options> optionsList = new ArrayList<>();
        String[] parts = data.split(";");

        for (String part : parts) {
            boolean isCorrect = part.endsWith("!");
            String content = isCorrect ? part.substring(0, part.length() - 1) : part;

            Options option = new Options();
            option.setContent(content);
            option.setIsCorrect(isCorrect ? "0" : "1"); // 假设0表示正确，1表示错误

            optionsList.add(option);
        }

        return optionsList;
    }
    public static String MD5(String password){
        if(password.length()!=32){
            //MD5加密密码后查询数据库
            password= DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        }else{
            password=password;
        }
        return password;
    }
    public String getClientIPAddress(HttpServletRequest request) {
        String ipAddress = request.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
        }

        // 对于可能存在的多个代理的情况，X-Forwarded-For可能会包含多个IP，此时取第一个IP
        if (ipAddress != null && ipAddress.contains(",")) {
            ipAddress = ipAddress.split(",")[0];
        }

        return ipAddress;
    }
}
